<?php

require_once("EKElement.php");
require_once("EKDocument.php");
require_once("EKParagraph.php");
require_once("EKParagraphRelationship.php");

class EKTalk extends EKElement
{
	private $id;
	private $authorId;
	private $ownerId;
	private $updateTime;
	private $content;
	private $enabled;
	private $talkType;
	
	private $ratingIds;
	
	private $ratingAValue;
	private $ratingAWeight;
	private $ratingBValue;
	private $ratingBWeight;
	
	//document constructor
	function __construct($id)
	{	
		$node = EKTalk::getTalkXmlNode($id);
		$this->id = $id;
		$this->enabled = $node->getAttribute("enabled");
		$this->talkType = $node->getAttribute("talkType");
		$this->authorId = $node->getElementsByTagName("authorId")->item(0)->nodeValue;
		$this->ownerId = $node->getElementsByTagName("ownerId")->item(0)->nodeValue;
		$this->updateTime = $node->getElementsByTagName("updateTime")->item(0)->nodeValue;
		$this->content = $node->getElementsByTagName("content")->item(0)->nodeValue;
		
		$this->ratingAValue = $node->getAttribute("ratingAValue");
		$this->ratingAWeight = $node->getAttribute("ratingAWeight");
		$this->ratingBValue = $node->getAttribute("ratingBValue");
		$this->ratingBWeight = $node->getAttribute("ratingBWeight");
		
		$ratingIdSet = $node->getElementsByTagName("ratingId");
		foreach ($ratingIdSet as $ratingIdInstance) {
	        $this->ratingIds[] = $ratingIdInstance->nodeValue;
		}
	}
	
	//get the xml node by talkId
	static function getTalkXmlNode($id){
		$xmlfile = SERVERLOCATION."data/EKTalk.xml";
		$doc = DOMDocument::load($xmlfile);
		$doc->validate();
		$node = $doc->getElementById($id);
		return $node;
	}
	
	static function getContentById($id){
		$node = EKTalk::getTalkXmlNode($id);
		return $node->getElementsByTagName("content")->item(0)->nodeValue;	
	}
	
	static function getOwnerIdById($id){
		$node = EKTalk::getTalkXmlNode($id);
		return $node->getElementsByTagName("ownerId")->item(0)->nodeValue;	
	}
	
	static function getTalkTypeById($id){
		$node = EKTalk::getTalkXmlNode($id);
		return $node->getAttribute("talkType");
	}
	
	static function getUpdateTimeById($id){
		$node = EKTalk::getTalkXmlNode($id);
		return date("Y/m/d[H:i:s]", $node->getElementsByTagName("updateTime")->item(0)->nodeValue);
	}
	
	static function getRatingAValue($id){
		$node = EKTalk::getTalkXmlNode($id);
		return $node->getAttribute("ratingAValue");
	}
	
	static function getRatingBValue($id){
		$node = EKTalk::getTalkXmlNode($id);
		return $node->getAttribute("ratingBValue");
	}
	# Get rating_A_value by id
	static function getRatingAValueById($id){
		$node = EKTalk::getTalkXmlNode($id);
		return $node->getAttribute("ratingAValue");
	}
	# Get rating_A_weight by id
	static function getRatingAWeightById($id){
		$node = EKTalk::getTalkXmlNode($id);
		return $node->getAttribute("ratingAWeight");
	}
	# Get rating_B_value by id
	static function getRatingBValueById($id){
		$node = EKTalk::getTalkXmlNode($id);
		return $node->getAttribute("ratingBValue");
	}
	# Get rating_B_weight by id
	static function getRatingBWeightById($id){
		$node = EKTalk::getTalkXmlNode($id);
		return $node->getAttribute("ratingBWeight");
	}
	
	//test if current document is enabled
	function ifEnabled(){
		if($this->enabled = "true"){
			return true;
		} else {return false;}
	}
	
	static function writeNewTalk($ownerId, $content, $talkType)
	{
		// 	begin to write talk contents
		$xmlfile = SERVERLOCATION."data/EKTalk.xml";
	
	    $doc = DOMDocument::load($xmlfile);
	    $root = $doc->getElementsByTagName("talkRoot")->item(0);
	
	    $newTalk = $doc->createElement("talk");
	    
	    $newTalkId = EKStatistics::getTalkLastId();
	
	    $newTalk->setAttribute("id", $newTalkId);
	    $newTalk->setAttribute("enabled", "true");
		$newTalk->setAttribute("talkType", $talkType);
		$newTalk->setAttribute("ratingAValue", 0);
		$newTalk->setAttribute("ratingAWeight", 1);
		$newTalk->setAttribute("ratingBValue", 0);
		$newTalk->setAttribute("ratingBWeight", 1);
	
		$authorId = $doc->createElement("authorId");
		$authorIdText = $doc->createTextNode($_SESSION["userid"]);
		$authorId->appendChild($authorIdText);
	   
		$ownerIdNode = $doc->createElement("ownerId");
		$ownerIdNodeText = $doc->createTextNode($ownerId);
		$ownerIdNode->appendChild($ownerIdNodeText);
	   
		$updateTime = $doc->createElement("updateTime");
		$updateTimeText = $doc->createTextNode(time());
		$updateTime->appendChild($updateTimeText);
	   
		$contentNode = $doc->createElement("content");
		$contentNodeText = $doc->createTextNode($content);
		$contentNode->appendChild($contentNodeText);
	   
		$newTalk->appendChild($authorId);
		$newTalk->appendChild($ownerIdNode);
		$newTalk->appendChild($updateTime);
		$newTalk->appendChild($contentNode);
	   
		$root->appendChild($newTalk);
	
		$doc->save($xmlfile);
		// end of writing talk contents
	   
		// begin to update the owner
		if($talkType == "p")
		{
			$xmlfile = SERVERLOCATION."data/EKParagraph.xml";
		}else if($talkType == "d")
		{
	   		$xmlfile = SERVERLOCATION."data/EKDocument.xml";
		}else{
	   		return;
		}
		$doc = DOMDocument::load($xmlfile);
		$owner = $doc->getElementByID($ownerId);
		$newTalkIdNode =$doc->createElement("talkId");
	    $newTalkIdNodeText =$doc->createTextNode($newTalkId);
	    $newTalkIdNode->appendChild($newTalkIdNodeText);  
	    if($preTalkId = $owner->getElementsByTagName("talkId")->item(0)){
	    	$owner->insertBefore($newTalkIdNode, $preTalkId);;
	    } else if($preTalkId = $owner->getElementsByTagName("ratingId")->item(0)){
	    	$owner->insertBefore($newTalkIdNode, $preTalkId);
	    } else if($preTalkId = $owner->getElementsByTagName("childId")->item(0)){
	    	$owner->insertBefore($newTalkIdNode, $preTalkId);
	    } else {
	    	$owner->appendChild($newTalkIdNode);
	    }
	    $doc->save($xmlfile);
	    // end of updating the owner
	    return $newTalkId;
	}
	
	# Delete current node
	static function deleteTalkNode($id){
		$xmlfile = SERVERLOCATION."data/EKTalk.xml";
		$doc = DOMDocument::load($xmlfile);
		$doc->validate();
		$documentRoot = $doc->getElementsByTagName("talkRoot")->item(0);
		$thisDoc = $doc->getElementById($id);		
		
		# this function is only called when the owner is deleted
		# so it does NOT delete reference of owner to it
		
		$t = new EKTalk($id);
		
		# delete the rating and talk referred to it
		if(count($t->ratingIds)>0){
		foreach($t->ratingIds as $a){
			EKRating::deleteRatingNode($a);
		}}

		$documentRoot->removeChild($thisDoc);
		EKStatistics::decTalkSumNum();
		EKUser::delRecord($t->authorId, "t", $id);
		$doc->save($xmlfile);
	}
	
	# do not check the user session
	# so the session should be checked before it is called
	static function administrate($id, $action){
		switch($action)
		{
			case "deny":
				EKTalk::denyTalkNode($id);
				break;
			default:
				return "The action '".$action."' on talk id = ".$id." is not permitted!";
		}
		return "The action '".$action."' on talk id = ".$id." performed.";
	}
	
	# Deny current node
	static function denyTalkNode($id){
		$xmlfile = SERVERLOCATION."data/EKTalk.xml";
		$doc = DOMDocument::load($xmlfile);
		$doc->validate();
		$documentRoot = $doc->getElementsByTagName("talkRoot")->item(0);
		$thisDoc = $doc->getElementById($id);		
		
		# this function is only called when the owner is deleted
		# so it does NOT delete reference of owner to it
		
		$t = new EKTalk($id);
		
		$event = new EKEvent($t->authorId, "deny", "t", $id);
		$event->save();
		
		$documentRoot->removeChild($thisDoc);
		EKStatistics::decTalkSumNum();
		EKUser::delRecord($t->authorId, "t", $id);
		$doc->save($xmlfile);

		# delete the owner reference to it
		switch($t->talkType)
		{
			case "d":
				EKElement::deleteRefIdById($t->ownerId, $id, "EKDocument", "talkId");
				break;
			case "p":
				EKElement::deleteRefIdById($t->ownerId, $id, "EKParagraph", "talkId");
				break;
		}
		
		# delete the rating referred to it
		if(count($t->ratingIds)>0){
		foreach($t->ratingIds as $a){
			EKRating::deleteRatingNode($a);
		}}
	}

}
?>